package com.centerm.bluetooth.receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.centerm.bluetooth.core.controller.constants.Constants;

/**
 * Created by linwenhui on 2017/4/28.
 */

public abstract class CMDParseReceiver extends BroadcastReceiver {
    private boolean unReg;

    public CMDParseReceiver() {
        this(true);
    }

    public CMDParseReceiver(boolean unReg) {
        this.unReg = unReg;
    }

    @Override
    public final void onReceive(Context context, Intent intent) {
        if (unReg)
            LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
        if (intent.hasExtra(Constants.PARAMS)) {
            byte[] params = intent.getByteArrayExtra(Constants.PARAMS);
            onReceive(context, intent, params, params.length);
        } else if (intent.hasExtra(Constants.ALL_PARAMS)) {
            onReceive(context, intent, null, 0);
        }
    }

    public abstract void onReceive(Context context, Intent intent, byte[] params, int paramLen);
}
